'''
Created on 2011-9-20

@author: Administrator
'''
import taskresult.base
import xml.dom.minidom
import datetime
import logging

def Parser(xmlfile):
    resultlist = []
    try:
        doc = xml.dom.minidom.parse(xmlfile)
        for v in doc.getElementsByTagName('testsuites'):
            for sv in v.getElementsByTagName('testsuite'):
                for tv in sv.getElementsByTagName('testcase'):
                    ci  = taskresult.base.CaseInfo()
                    ci.name = sv.getAttribute('name') + '.' + tv.getAttribute('name')
                    ci.runtime = tv.getAttribute('time')
                    ci.start_time = datetime.datetime.now()
                    ci.end_time = datetime.datetime.now() + datetime.timedelta(milliseconds = (float(ci.runtime) * 1000) )
                    failure =   tv.getElementsByTagName('failure')
                    if failure:
                        ci.result = taskresult.base.FAILURE
                        ci.message = failure[0].getAttribute('message') + '\n'  + failure[0].firstChild.wholeText 
                    else:
                        ci.result = taskresult.base.PASS
                    resultlist.append(ci)
    except Exception as e:
        logging.exception(e)
    return resultlist
